import { BaseValue } from "../data/BaseValue";
import { BaseUtil } from "../util/BaseUtil";
import { BulletManager } from "./BulletManager";
import Game from "./Game";
import { Constants } from "../data/Constants";
import { AudioManager } from "./AudioManager";

const { ccclass, property } = cc._decorator;

@ccclass
export class BottomManager extends cc.Component {

    @property({
        type: BulletManager
    })
    bulletManager: BulletManager = null;

    @property({
        type: cc.SpriteAtlas
    })
    cannonAtlas: cc.SpriteAtlas = null;

    @property({
        type: cc.Sprite
    })
    cannon: cc.Sprite = null;

    @property({
        type: cc.Label
    })
    goldCoins: cc.Label = null;

    private _currFrame = 1;

    private _openFireTime = 0;

    onLoad() {
        this.cannon.spriteFrame = this.cannonAtlas.getSpriteFrame(`cannon_${BaseValue.CANNON_LEVEL}_1`);
    }

    public _openFire(isFire = false, event?: cc.Event.EventTouch) {
        BaseValue.IS_FIRE = isFire;
        if (!isFire) {
            this.cannon.spriteFrame = this.cannonAtlas.getSpriteFrame(`cannon_${BaseValue.CANNON_LEVEL}_1`);
            return;
        }
        if (BaseUtil.getTimestamp() - this._openFireTime > BaseValue.FIRE_INTERVALS) {

            this.goldCoins.string = String(BaseValue.GOLD_COINS)

            this._openFireTime = BaseUtil.getTimestamp();
            AudioManager.playEffect(Constants.EffectSource.GUN_SHOT);
            let degress = null;

            // 获取炮台位置
            let cannonPos = this.cannon.node.getPosition();
            // 如果触摸事件不为空
            if (!BaseUtil.isEmpty(event)) {
                // 将触摸点从世界坐标转到节点坐标
                let touchPos = this.node.convertToNodeSpaceAR(event.getLocation());

                // 如果触摸位置在炮台之下则不转动
                if (touchPos.y < cannonPos.y) {
                    return;
                }

                // 获取触摸点和炮台点的弧度
                let radian = Math.atan((touchPos.x - cannonPos.x) / (touchPos.y - cannonPos.y));
                degress = -cc.misc.radiansToDegrees(radian) + 360;
                this.cannon.node.angle = degress;

            } else {
                degress = this.cannon.node.angle;
            }

            let hu = (2 * Math.PI / 360) * (degress - 270);

            // 炮弹起始位置
            let startPos = cc.v2();
            startPos.x = cannonPos.x + Math.cos(hu) * 50
            startPos.y = cannonPos.y + Math.sin(hu) * 50

            // 炮弹运动结束位置(计算得出在屏幕之外)
            let endPos = cc.v2();
            endPos.x = cannonPos.x + Math.cos(hu) * 2000;
            endPos.y = cannonPos.y + Math.sin(hu) * 2000;
            this.bulletManager.createBullet(startPos, endPos, degress);
        }
    }

    /**
     * 子弹开火动画
     */
    public _fireAnim() {
        if (!BaseValue.IS_FIRE) {
            return;
        }
        let frame = this.cannonAtlas.getSpriteFrame(`cannon_${BaseValue.CANNON_LEVEL}_${this._currFrame}`);
        this._currFrame++;
        if (BaseUtil.isEmpty(frame)) {
            frame = this.cannonAtlas.getSpriteFrame(`cannon_${BaseValue.CANNON_LEVEL}_1`);
            this._currFrame = 1;
        }
        this.cannon.spriteFrame = frame;
    }

    /**
     * 升级
     */
    public cannonUpgrade() {
        BaseValue.CANNON_LEVEL++;
        if (BaseValue.CANNON_LEVEL > Constants.CANNON_MAX_LEVEL) {
            BaseValue.CANNON_LEVEL = 1;
        }
        this.cannon.spriteFrame = this.cannonAtlas.getSpriteFrame(`cannon_${BaseValue.CANNON_LEVEL}_1`);
    }

    /**
     * 降级
     */
    public cannonDegrade() {
        BaseValue.CANNON_LEVEL--;
        if (BaseValue.CANNON_LEVEL <= 0) {
            BaseValue.CANNON_LEVEL = Constants.CANNON_MAX_LEVEL;
        }
        this.cannon.spriteFrame = this.cannonAtlas.getSpriteFrame(`cannon_${BaseValue.CANNON_LEVEL}_1`);
    }

}
